<?php

namespace App\Traits;
use Illuminate\Http\Resources\Json\Resource;

trait BaseResourceCollection {

	/**
	 * @var array
	 */
	protected $withoutFields = [], $stamp = "default";

	/**
	 * Transform the resource collection into an array.
	 *
	 * @param  \Illuminate\Http\Request
	 * @return array
	 */
	public function toArray($request) {
		return $this->processCollection($request);
	}

	public function hide(array $fields) {
		$this->withoutFields = $fields;
		return $this;
	}

	public function type(string $type) {
		$this->stamp = $type;
		return $this;
	}

	/**
	 * Send fields to hide to UsersResource while processing the collection.
	 *
	 * @param $request
	 * @return array
	 */
	protected function processCollection($request) {
		return $this->collection->map(function (Resource $resource) use ($request) {
			return $resource->type($this->stamp)->hide($this->withoutFields)->toArray($request);
		})->all();
	}
}